//package com.navi.loaddata.loadService.impl.measureData.cf;
//
//import com.navi.loaddata.core.errorLog.ErrorLog;
//import com.navi.loaddata.core.etlException.ETLException;
//import com.navi.loaddata.core.gpEntity.MppLayoutPnlD;
//import com.navi.loaddata.core.gpEntity.cf.WppFdefectF;
//import com.navi.loaddata.core.gpEntity.cf.WppFdefectGlassF;
//import com.navi.loaddata.core.gpEntity.cf.WppFdefectPanelF;
//import com.navi.loaddata.core.mesEntity.MesProducthistory;
//import com.navi.loaddata.core.mysqlEntity.EtlConfD;
//import com.navi.loaddata.core.oracleEntity.cf.EtlCfChipV;
//import com.navi.loaddata.core.oracleEntity.cf.EtlCfDefectV;
//import com.navi.loaddata.core.oracleEntity.cf.EtlCfCmnGlassV;
//import com.navi.loaddata.core.oracleEntity.cf.EtlCfGlassV;
//import com.navi.loaddata.core.oracleEntity.cf.EtlCfProductV;
//import com.navi.loaddata.core.oracleEntity.cf.EtlCfStepV;
//import com.navi.loaddata.core.repository.impl.OracleRepositoryImpl;
//import com.navi.loaddata.core.util.LoggerUtil;
//import com.navi.loaddata.core.util.ProcessUtil;
//import com.navi.loaddata.loadService.impl.baseData.cf.LoadMesBaseDataServiceCF;
//import com.navi.loaddata.loadService.impl.baseData.cf.ProcessBaseDataServiceCF;
//import org.hibernate.Query;
//import org.hibernate.SQLQuery;
//import org.hibernate.Session;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Service;
//import org.springframework.transaction.annotation.Transactional;
//
//import java.util.ArrayList;
//import java.util.List;
//import java.util.Map;
//
//import static com.navi.loaddata.core.constant.GenericDef.SHOP_CF;
//import static java.util.stream.Collectors.groupingBy;
//
///**
// * @author jay
// */
//@Service
//@Transactional(transactionManager = "thirdaryPlatformTransactionManager",
//        rollbackFor = {RuntimeException.class, Exception.class})
//public class LoadMeasureDataServiceCF {
//    private static final Logger logger = LoggerFactory.getLogger(LoadMeasureDataServiceCF.class);
//    private LoggerUtil loggerUtil = new LoggerUtil(this.getClass());
//
//    @Autowired
//    private OracleRepositoryImpl dao3;
//
//    @Autowired
//    private LoadMesBaseDataServiceCF loadMesBaseDataService;
//
//    @Autowired
//    private ProcessBaseDataServiceCF processBaseDataService;
//    @Autowired
//    private ProcessDefectDataServiceCF processDefectDataService;
//    @Autowired
//    private LoadDefectInfoServiceCF loadDefectInfoService;
//
//
//    // call by LoadJobMain  @Scheduled(cron = "*/5 * * * * ?")
//    public int loadMeasureDataMain(EtlConfD etlConfD) throws ETLException {
//        int countSize = 0;
//        Session session = dao3.getEntityManagerSession();
//        try {
//            countSize = loadMeasureData(session, etlConfD);
//            logger.info("LoadMeasureDataMain finish");
//        } catch (ETLException e) {
//            throw new ETLException(e.getMessage());
//        }
//        return countSize;
//    }
//
//    /**
//     * Load Measure Data
//     * <p>
//     * load base -> session table
//     * load defect data  -> session table
//     * call base proc  -> auto commit
//     * call defect proc -> no auto commit
//     * delete defect session table
//     *
//     * @param session
//     * @param etlConfD
//     */
//    private int loadMeasureData(Session session, EtlConfD etlConfD) throws ETLException {
//        int countSize = 0;
//
//
//        // defect
//        List<WppFdefectGlassF> defectGlassFList = loadDefectInfoService.loadGlassInfo(etlConfD);
//
//        logger.info("CF defect glass count {}", defectGlassFList.size());
//        if (!defectGlassFList.isEmpty()) {
//            // base
//            List<EtlCfStepV> etlStepVList = new ArrayList<>();
//            List<EtlCfProductV> etlProductVList = new ArrayList<>();
//            try {
//                etlStepVList = loadMesBaseDataService.loadAllFinalStep(SHOP_CF, ErrorLog.ERROR_PROCESS_ARRAY_STEP_V, logger);
//                etlProductVList = loadMesBaseDataService.loadAllFinalProduct(SHOP_CF, ErrorLog.ERROR_PROCESS_ARRAY_PRODUCT_V, logger);
//            } catch (Exception e) {
//                logger.error("Cannot excute loadAll()_BASE from [{}] to [{}]。", etlConfD.getRunStartTimestamp(), etlConfD.getRunEndTimestamp());
//                throw new ETLException(e.getMessage());
//            }
//            // save base
//            try {
//                saveAll(etlStepVList, ErrorLog.ERROR_TABLE_ETL_ARRAY_STEP_V, "NULL");
//                saveAll(etlProductVList, ErrorLog.ERROR_TABLE_ETL_ARRAY_PRODUCT_V, "NULL");
//            } catch (Exception e) {
//                logger.error("Cannot excute dao.saveAll()_BASE from [{}] to [{}]。", etlConfD.getRunStartTimestamp(), etlConfD.getRunEndTimestamp());
//                throw new ETLException(e.getMessage());
//            }
//
//            // call base proc
//            try {
//                SQLQuery queryLoadStep = session.createSQLQuery("{call CF_BASE_LOADER.LOAD_STEP()}");
//                queryLoadStep.executeUpdate();
//            } catch (Exception e) {
//                logger.error(e.getMessage());
//                logger.error(LoggerUtil.stackTraceToString(e));
//                logger.error("Cannot call procedure from [{}] to [{}]。", etlConfD.getRunStartTimestamp(), etlConfD.getRunEndTimestamp());
//                throw new ETLException("ALL" + ErrorLog.ERROR_LOAD_ARRAY_STEP_V + e.getMessage());
//            }
//            try {
//                SQLQuery queryLoadProduct = session.createSQLQuery("{call CF_BASE_LOADER.LOAD_PRODUCT()}");
//                queryLoadProduct.executeUpdate();
//            } catch (Exception e) {
//                logger.error(e.getMessage());
//                logger.error(LoggerUtil.stackTraceToString(e));
//                logger.error("Cannot call procedure from [{}] to [{}]。", etlConfD.getRunStartTimestamp(), etlConfD.getRunEndTimestamp());
//                throw new ETLException("ALL" + ErrorLog.ERROR_LOAD_ARRAY_PRODUCT_V + e.getMessage());
//            }
//
//
//            Map<String, List<WppFdefectGlassF>> glassDataTree =
//                    defectGlassFList.stream().collect(groupingBy(WppFdefectGlassF::getGlassId));
//            for (Map.Entry<String, List<WppFdefectGlassF>> entry : glassDataTree.entrySet()) {
//                List<WppFdefectGlassF> curGlassList = entry.getValue();
//                //common
//                List<MesProducthistory> cmnGlassVList =
//                        loadMesBaseDataService.loadCommonGlassInfo(etlConfD, curGlassList.get(0).getGlassId());
//                if (cmnGlassVList.size() == 0) {
////                    System.out.println(curGlassList.get(0).getGlassId()+"++++++++++++++++++++++++++++++++++++++++++++");
//                    continue;
//                }
////                System.out.println(curGlassList.get(0).getGlassId()+"===============================================");
//                List<EtlCfCmnGlassV> etlCmnGlassVList = new ArrayList<>();
//                try {
//                    etlCmnGlassVList = processBaseDataService.processCommonGlass(cmnGlassVList, ErrorLog.ERROR_PROCESS_ARRAY_CMN_GLASS_V, logger);
//                } catch (Exception e) {
//                    logger.error("Cannot excute processCMN() from [{}] to [{}]。", etlConfD.getRunStartTimestamp(), etlConfD.getRunEndTimestamp());
//                    throw new ETLException(curGlassList.get(0).getGlassId() + ":" + e.getMessage());
//                }
////                logger.info("Array common glass count {}", cmnGlassVList.size());
//                // save
//                try {
//                    saveAll(etlCmnGlassVList, ErrorLog.ERROR_TABLE_ETL_ARRAY_CMN_GLASS_V, curGlassList.get(0).getGlassId());
//                } catch (Exception e) {
//                    logger.error("Cannot excute dao.saveCMN() from [{}] to [{}]。", etlConfD.getRunStartTimestamp(), etlConfD.getRunEndTimestamp());
//                    throw new ETLException(e.getMessage());
//                }
//                // call procedure
//                try {
//                    SQLQuery queryLoadCmnGlass = session.createSQLQuery("{call CF_MES_LOADER.LOAD_CMN_GLASS()}");
//                    queryLoadCmnGlass.executeUpdate();
//                } catch (Exception e) {
//                    logger.error(LoggerUtil.stackTraceToString(e));
//                    logger.error("Cannot call procedure from [{}] to [{}]。", etlConfD.getRunStartTimestamp(), etlConfD.getRunEndTimestamp());
//                    throw new ETLException(curGlassList.get(0).getGlassId() + ":" + ErrorLog.ERROR_LOAD_ARRAY_CMN_GLASS_V + ":" + e.getMessage());
//                }
//
//                //getMap坐标
//                List<MppLayoutPnlD> layoutPnlDS=new ArrayList<>();
//                try{
//                    layoutPnlDS=loadDefectInfoService.big2small(curGlassList.get(0));
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
//
//
//                for (int i = 0; i < curGlassList.size(); i++) {
//                    //站点限制
//                    if (true == (ProcessUtil.opeLimit(curGlassList.get(i).getOpeId()))) {
//                        continue;
//                    }
//                    Map<String, String> mOpeDList =
//                            loadDefectInfoService.loadMOpeDInfo(etlConfD);
//                    //panel
//                    List<WppFdefectPanelF> defectPanelFEntityList =
//                            loadDefectInfoService.loadPanelInfo(etlConfD, curGlassList.get(i));
//                    logger.info("Array defect panel count {}", defectPanelFEntityList.size());
//                    //defect
//                    List<WppFdefectF> defectFEntityList =
//                            loadDefectInfoService.loadDefectInfo(etlConfD, curGlassList.get(i));
//                    logger.info("Array defect count {}", defectFEntityList.size());
//
//
//                    List<EtlCfGlassV> etlCfGlassVList = new ArrayList<>();
//                    List<EtlCfChipV> etlCfChipVList = new ArrayList<>();
//                    List<EtlCfDefectV> etlCfDefectVList = new ArrayList<>();
//
//                    try {
//                        etlCfGlassVList = processDefectDataService.processGlass(curGlassList.get(i), ErrorLog.ERROR_PROCESS_ARRAY_GLASS_V, logger);
//                        etlCfChipVList = processDefectDataService.processPanel(defectPanelFEntityList, ErrorLog.ERROR_PROCESS_ARRAY_CHIP_V, logger);
//                        etlCfDefectVList = processDefectDataService.processDefect(layoutPnlDS,defectFEntityList, mOpeDList, ErrorLog.ERROR_PROCESS_ARRAY_DEFECT_V, logger);
//                    } catch (Exception e) {
//                        logger.error("Cannot excute process() from [{}] to [{}]。", etlConfD.getRunStartTimestamp(), etlConfD.getRunEndTimestamp());
//                        throw new ETLException("process错误：" + curGlassList.get(i).getGlassId() + ":" + e.getMessage());
//                    }
//                    // save
//                    try {
//                        saveAll(etlCfGlassVList, ErrorLog.ERROR_TABLE_ETL_ARRAY_GLASS_V, curGlassList.get(i).getGlassId());
//                        saveAll(etlCfChipVList, ErrorLog.ERROR_TABLE_ETL_ARRAY_CHIP_V, curGlassList.get(i).getGlassId());
//                        saveAll(etlCfDefectVList, ErrorLog.ERROR_TABLE_ETL_ARRAY_DEFECT_V, curGlassList.get(i).getGlassId());
//                    } catch (Exception e) {
//                        logger.error("Cannot excute dao.saveAll() from [{}] to [{}]。", etlConfD.getRunStartTimestamp(), etlConfD.getRunEndTimestamp());
//                        throw new ETLException(e.getMessage());
//                    }
//
//                    // call proc
//                    try {
//                        SQLQuery queryDefect = session.createSQLQuery("{Call CF_FILE_LOADER.LOAD_DATA()}");
//                        queryDefect.executeUpdate();
//                    } catch (Exception e) {
//                        logger.error(ErrorLog.ERROR_LOAD_ARRAY_DATA + ":" + e.getMessage());
//                        logger.error(LoggerUtil.stackTraceToString(e));
//                        logger.error("Cannot call procedure from [{}] to [{}]。", etlConfD.getRunStartTimestamp(), etlConfD.getRunEndTimestamp());
//                        throw new ETLException(curGlassList.get(i).getGlassId() + ":" + ErrorLog.ERROR_LOAD_ARRAY_DATA + ":" + e.getMessage());
//                    }
//                    countSize++;
//                    try {
//                        String hqlGlass = "Delete FROM EtlCfGlassV";
//                        String hqlPanel = "Delete FROM EtlCfChipV";
//                        String hqlDefect = "Delete FROM EtlCfDefectV";
//                        Query qGlass = session.createQuery(hqlGlass);
//                        Query qPanel = session.createQuery(hqlPanel);
//                        Query qDefect = session.createQuery(hqlDefect);
////                        Query qCMN = session.createQuery(hqlCMN);
//                        qGlass.executeUpdate();
//                        qPanel.executeUpdate();
//                        qDefect.executeUpdate();
//                    } catch (Exception e) {
//                        logger.error(LoggerUtil.stackTraceToString(e));
//                        throw new ETLException("删除DEFECT错误:" + e.getMessage());
//                    }
//                }
//                try {
//                    String hqlCMN = "Delete FROM EtlCfCmnGlassV";
//                    Query qCMN = session.createQuery(hqlCMN);
//                    qCMN.executeUpdate();
//                } catch (Exception e) {
//                    logger.error(LoggerUtil.stackTraceToString(e));
//                    throw new ETLException("删除CMN错误:" + e.getMessage());
//                }
//            }
//
//        } else {
//            logger.info("没有此区间的数据。"); // todo print log & save etl data
//        }
//        return countSize;
//    }
//
//    public <T> void saveAll(List<T> entities, int errorViewLog, String glassId) throws ETLException {
//        try {
//            dao3.saveAll(entities);
//        } catch (Exception e) {
//            logger.error(errorViewLog + ":" + e.getMessage());
//            logger.error(LoggerUtil.stackTraceToString(e));
//            throw new ETLException(glassId + ":" + errorViewLog + ":" + e.getMessage());
//        }
//    }
//}
